const { By, until } = require('selenium-webdriver')

module.exports = function getComment (driver, appid) {
  const target = By.css('#comment .ivu-table-body td.ivu-table-column-center')
  
  return driver.get(`https://www.qimai.cn/andapp/comment/appid/${appid}`)
  .then(() => {
    return driver.wait(until.elementsLocated(target))
  })
  .then(() => {
    return driver.findElements(target)
  })
  .then((scores) => {
    const scoreTasks = []
    scores.forEach((score, index) => {
      if (index === 1 || index === 4 || index === 7) {
        scoreTasks.push(score.getText())
      }
    })
    return Promise.all(scoreTasks)
  })
  .then(([baidu, tencent, qh360]) => {
    return {
      baidu,
      tencent,
      qh360
    }
  })
}